package com.mes.schedule.algorithm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.mes.schedule.algorithm.iface.IPriorityCalc;
import com.mes.schedule.domain.SOperationTask;
import com.mes.schedule.domain.SPartTask;
import com.mes.schedule.domain.ScheduleScheme;

/*import com.mes.domain.schedule.SOperationTask;
import com.mes.domain.schedule.SPartTask;
import com.mes.domain.schedule.ScheduleScheme;*/

@Component("priorityCalc")
public class PriorityCalc implements IPriorityCalc{

	private static final Log log = LogFactory
			.getLog(PriorityCalc.class);



	public void calcPriority(ScheduleScheme scheme) {

		this.calcPriority(scheme.getAllOperationTaskMap().values(), scheme
				.getAllPartTaskMap().values());
		// // 重新计算松弛率
		// for (SPartTask partTask : scheme.getAllPartTaskMap().values()) {
		// if (partTask.getLateFinish() != null) {
		// partTask.setControlFinish(partTask.getLateFinish());
		// } else
		// partTask.setControlFinish(scheme.getScheduleFinish());
		//
		// }
		//		
		//		
		// if(operationTasks==null||operationTasks.size()==0)return;
		// for (SOperationTask operationTask : operationTasks) {
		// if (operationTask.getLateFinish() != null) {
		// operationTask.setControlFinish(operationTask.getLateFinish());
		// } else
		// operationTask.setControlFinish(scheme.getScheduleFinish());
		// this.calcSlack(operationTask);
		// }

		// for (SPartTask partTask : scheme.getAllPartTaskMap().values()) {
		// insertOrUpdate(partTask);
		// for (SOperationTask operationTask : partTask.getOperationTaskList())
		// {
		// insertOrUpdate(operationTask);
		// }
		// }
		//		
		// this.getKsession().fireAllRules();
		// //清除drools中的对象
		// for (SPartTask partTask : scheme.getAllPartTaskMap().values()) {
		// this.delete(partTask);
		// for (SOperationTask operationTask : partTask.getOperationTaskList())
		// {
		// this.delete(operationTask);
		// }
		// }

	}

	public void calcOperationTaskPriority(
			Collection<? extends SOperationTask> operationTaskList) {
		for (SOperationTask operationTask : operationTaskList) {
			calcOperationTaskPriority(operationTask);
		}

	}

	public void calcOperationTaskPriority(SOperationTask operationTask) {
		if(operationTask.getSlackFactor()<0)
			operationTask.setPriority(101);
		else if(operationTask.getSlackFactor()<1)
			operationTask.setPriority(101);
		else if(operationTask.getSlackFactor()<2)
			operationTask.setPriority(201);
		else if(operationTask.getSlackFactor()<3)
			operationTask.setPriority(301);
		else
			operationTask.setPriority(401);

	}

	public void calcPartTaskPriority(Collection<SPartTask> partTaskList) {
		for(SPartTask partTask:partTaskList)
		{
			calcPartTaskPriority(partTask);
		}
		// this.getKsession().dispose();这句话什么用？
	}

	public void calcPartTaskPriority(SPartTask partTask) {
		calcOperationTaskPriority(partTask.getOperationTaskList());

		
		List<SOperationTask> ol = partTask.getOperationTaskList();
		if (ol != null && ol.size() > 0)
			partTask.setPriority(ol.get(ol.size() - 1).getPriority());

	}



	public static void main(String[] args) {
		// SOperationTask operationTask = new SOperationTask();
		// operationTask.setSlackFactor(-3);
		// IPriorityCalc priorityCalc = new PriorityCalcDroolsImpl();
		// priorityCalc.calcOperationTaskPriority(operationTask);
		// System.out.println(">>>>>>>>" + operationTask.getPriority());
	}

 
	public void calcPriority(
			Collection<? extends SOperationTask> operationTaskList,
			Collection<SPartTask> partTaskList) {
		for(SOperationTask operationTask:operationTaskList)
		{
			operationTask.setPriority(999);
		}
		for (SPartTask partTask : partTaskList) {
			partTask.setPriority(999);
		}
		
		// TODO Auto-generated method stub
		// 待调度任务工序集合
		calcOperationTaskPriority(operationTaskList);

		// for (SPartTask partTask : partTaskList) {
		// List<SOperationTask> ol = partTask.getOperationTaskList();
		// if ( ol != null && ol.size() > 0)
		// partTask.setPriority(ol.get(0).getPriority());
		//
		// }
		for (SPartTask partTask : partTaskList) {
			List<SOperationTask> ol = partTask.getOperationTaskList();
			if (ol != null && ol.size() > 0)
				partTask.setPriority(ol.get(ol.size() - 1).getPriority());


		}

		//

		// /*
		// * 这一部分也需要放到规则引擎
		// */
		// for (SOperationTask operationTask : operationTaskList) {
		// switch(operationTask.getTaskState())
		// {
		// case 207://已开始
		// operationTask.setCalPriority(10);
		// break;
		// case 2071://已开始
		// operationTask.setCalPriority(10);
		// break;
		// case 2072://已开始
		// operationTask.setCalPriority(10);
		// break;
		// case 2073://已开始
		// operationTask.setCalPriority(10);
		// break;
		// case 208://已暂停
		// operationTask.setCalPriority(10);
		// break;
		// case 2069://已准备好
		// operationTask.setCalPriority(15);
		// break;
		// case 2081://已中断
		// operationTask.setCalPriority(18);
		// break;
		// case 2061://已开始准备
		// operationTask.setCalPriority(20);
		// break;
		// case 206://这个似乎不应该成一个状态位
		// operationTask.setCalPriority(50);
		// break;
		//
		// }
		//
		// }

	}

 
	public void calcSchedulePriority(ScheduleScheme scheme) {
		this.calcPriority(scheme.getScheduleOperationTasks(), scheme
				.getSchedulePartTasks());

	}
}
